package shop.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import shop.dao.AddressDao;
import shop.entity.Address;
import shop.util.DBUtil;

public class AddressDaoImpl implements AddressDao {

	@Override
	public Address findDefault(int uid) {
		Address address = null;
		String sql = "select * from t_user_address where user_id="+uid+" and is_default = 1";
		ResultSet rs = DBUtil.query(sql, null);
		try {
			if(rs.next()) {
				String name = rs.getString("name");
				String address1 = rs.getString("address");
				String tel = rs.getString("tel");
				String sign = rs.getString("sign");
				String email = rs.getString("email");
				String postalCode = rs.getString("postal_code");
				String phone = rs.getString("phone");
				Date deliverTime = rs.getDate("deliver_time");
				int id = rs.getInt("id");
				int userId = rs.getInt("user_id");
				int isDefault = rs.getInt("is_default");
				String remark = rs.getString("remark");
				Date createTime = rs.getDate("create_time");
				address = new Address(id, userId, address1, tel, phone, postalCode, email, name, sign, deliverTime, createTime, isDefault, remark);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return address;
	}

	@Override
	public List<Address> findAll(int id) {
		List<Address>  list = new ArrayList<>();
		String sql = "select * from t_user_address where user_id="+id;
		ResultSet rs = DBUtil.query(sql, null);
		try {
			while(rs.next()) {
				String name = rs.getString("name");
				String address1 = rs.getString("address");
				String tel = rs.getString("tel");
				String sign = rs.getString("sign");
				String email = rs.getString("email");
				String postalCode = rs.getString("postal_code");
				String phone = rs.getString("phone");
				Date deliverTime = rs.getDate("deliver_time");
				int id1 = rs.getInt("id");
				int userId = rs.getInt("user_id");
				int isDefault = rs.getInt("is_default");
				String remark = rs.getString("remark");
				Date createTime = rs.getDate("create_time");
				Address address = new Address(id1, userId, address1, tel, phone, postalCode, email, name, sign, deliverTime, createTime, isDefault, remark);
				list.add(address);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public Address findById(int id) {
		Address address = null;
		String sql = "select * from t_user_address where id="+id;
		ResultSet rs = DBUtil.query(sql, null);
		try {
			if(rs.next()) {
				String name = rs.getString("name");
				String address1 = rs.getString("address");
				String tel = rs.getString("tel");
				String sign = rs.getString("sign");
				String email = rs.getString("email");
				String postalCode = rs.getString("postal_code");
				String phone = rs.getString("phone");
				Date deliverTime = rs.getDate("deliver_time");
				int id1 = rs.getInt("id");
				int userId = rs.getInt("user_id");
				int isDefault = rs.getInt("is_default");
				String remark = rs.getString("remark");
				Date createTime = rs.getDate("create_time");
				address = new Address(id1, userId, address1, tel, phone, postalCode, email, name, sign, deliverTime, createTime, isDefault, remark);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return address;
	}

	@Override
	public void edit(Address addr) {
		String sql = "UPDATE `interesting_shop_db`.`t_user_address` SET `user_id` = ?, `address` = ?, `tel` = ?, `phone` = ?, `postal_code` = ?, `email` = ?, `name` = ?, `sign` = ?, `deliver_time` = ?,  `remark` = ? WHERE `id` = ?";
		Object[] params = new Object[] {addr.getUserId(),addr.getAddress(),addr.getTel(),addr.getPhone(),addr.getPostalCode(),addr.getEmail(),addr.getName(),addr.getSign(),addr.getDeliverTime(),addr.getRemark(),addr.getId()};
		DBUtil.update(sql, params);
	}

	@Override
	public void del(int id) {
		String sql = "delete from t_user_address where id = "+id;
		DBUtil.update(sql, null);
		
	}

	@Override
	public void add(Address addr) {
		String sql = "INSERT INTO `interesting_shop_db`.`t_user_address`( `user_id`, `address`, `tel`, `phone`, `postal_code`, `email`, `name`, `sign`, `deliver_time`,   `remark`) VALUES ( ?,?, ?, ?, ?, ?, ?, ?, ?, ?)";
		Object[] params = new Object[] {addr.getUserId(),addr.getAddress(),addr.getTel(),addr.getPhone(),addr.getPostalCode(),addr.getEmail(),addr.getName(),addr.getSign(),addr.getDeliverTime(),addr.getRemark()};
		DBUtil.update(sql, params);
	}

	@Override
	public void setDefault(int id) {
		String sql = "update t_user_address set is_default = 1 where id = "+id;
		DBUtil.update(sql, null);
	}

	@Override
	public void removeDefault(int id) {
		String sql = "update t_user_address set is_default = 0 where id = "+id;
		DBUtil.update(sql, null);
	}
	
}
